package com.lekang.service.impl;

import com.lekang.domain.Elderly;
import com.lekang.dto.BindFamilyDto;
import com.lekang.mapper.BindFamilyMapper;
import com.lekang.service.IBindFamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName BindFamilyService
 * @Description TODO
 * @Author wangjinyuan
 * @Date 2025年10月23日 16:58
 * @Version 1.0
 */
@Service
public class BindFamilyService implements IBindFamilyService {
    @Autowired
    private BindFamilyMapper bindFamilyMapper;
    @Override
    public String bindElderlyToFamily(BindFamilyDto bindFamilyDto) {
        Elderly elderly = bindFamilyMapper.selectElderlyByNameAndIdCard(bindFamilyDto);
        if (elderly == null) {
            return "当前老人未入住";
        } else {
            // 检查是否已绑定
            int existingCount = bindFamilyMapper.countByWxUserIdAndElderlyId(
                    bindFamilyDto.getWxUserId(),
                    elderly.getId()
            );
            if (existingCount > 0) {
                System.out.println("用户已绑定该老人");
                return "用户已绑定该老人";
            }
            // 调用Mapper层插入绑定关系
            System.out.println("准备插入的extraField1：" + bindFamilyDto.getExtraField1());
            int rowsAffected = bindFamilyMapper.insertBindFamily(
                    bindFamilyDto.getWxUserId(),
                    elderly.getId(),
                    bindFamilyDto.getExtraField1()
            );
            return rowsAffected > 0 ? "绑定成功" : "绑定失败";
        }
    }
    @Override
    public String unbindElderlyFromFamily(BindFamilyDto bindFamilyDto) {
           int rowsAffected = bindFamilyMapper.deleteBindFamily(
                    bindFamilyDto.getWxUserId(),
                   bindFamilyDto.getElderlyId()
            );
            return rowsAffected > 0 ? "解绑成功" : "解绑失败";
    }
}
